package com.manli.api.util;

import com.alibaba.fastjson.JSONObject;
import com.manli.api.dto.SysWechatConfig;
import com.manli.api.pojo.WeChatMsgTemplates;
import com.manli.wechat.utils.WeChat;
import com.manli.wechat.utils.wechattemplate.WeixinTemplateSender;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.SQLReady;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class WeChatPushMessageUtil {
    @Autowired
    private WeChatConfigUtil weChatConfigUtil;
    @Autowired
    private WeChatOverUtil weChatOverUtil;
    @Value("${wechat-appid}")
    private String appId;
    //商品上架审核失败
    public void goodsShenHeResultNo(String valueId,String openid){
        try{
            SysWechatConfig config = weChatConfigUtil.getWeChatByAppid(appId);
            WeChat weChat = new WeChat(config.getAppId(), config.getAppSecret());
            String accessToken = weChatOverUtil.getAccessToken(weChat);

            Map<String,Object> dataMap = new HashMap<>();
            Map<String,Object> first = new HashMap<>();
            first.put("value","您的商品上架审核失败");
            Map<String,Object> keyword1 = new HashMap<>();
            keyword1.put("value",valueId);
            keyword1.put("color","#173177");
            Map<String,Object> keyword2 = new HashMap<>();
            keyword2.put("value",DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
            Map<String,Object> keyword3 = new HashMap<>();
            keyword3.put("value","未通过");
            Map<String,Object> remark = new HashMap<>();
            remark.put("value","点击查看");

            dataMap.put("first",first);
            dataMap.put("keyword1",keyword1);
            dataMap.put("keyword2",keyword2);
            dataMap.put("keyword3",keyword3);
            dataMap.put("remark",remark);
            JSONObject jsonObject = new JSONObject(dataMap);
            WeixinTemplateSender.sendWechatmsgToUser(openid, WeChatMsgTemplates.onVerify,"",jsonObject,accessToken);
        }catch (Exception e){
            e.printStackTrace();
            log.error("推送商户失败",e);
        }

    }

    //商品上架审核成功
    public void goodsShenHeResultYes(String valueId,String openid){
        try{
            SysWechatConfig config = weChatConfigUtil.getWeChatByAppid(appId);
            WeChat weChat = new WeChat(config.getAppId(), config.getAppSecret());
            String accessToken = weChatOverUtil.getAccessToken(weChat);

            Map<String,Object> dataMap = new HashMap<>();
            Map<String,Object> first = new HashMap<>();
            first.put("value","您的商品上架审核成功");
            Map<String,Object> keyword1 = new HashMap<>();
            keyword1.put("value",valueId);
            keyword1.put("color","#173177");
            Map<String,Object> keyword2 = new HashMap<>();
            keyword2.put("value",DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
            Map<String,Object> keyword3 = new HashMap<>();
            keyword3.put("value","通过");
            Map<String,Object> remark = new HashMap<>();
            remark.put("value","点击查看");

            dataMap.put("first",first);
            dataMap.put("keyword1",keyword1);
            dataMap.put("keyword2",keyword2);
            dataMap.put("keyword3",keyword3);
            dataMap.put("remark",remark);
            JSONObject jsonObject = new JSONObject(dataMap);
            WeixinTemplateSender.sendWechatmsgToUser(openid,WeChatMsgTemplates.onVerify,"",jsonObject,accessToken);
        }catch (Exception e){
            e.printStackTrace();
            log.error("推送商户失败",e);
        }

    }

    //拼团成功
    public void groupSuccess(String goodsName,String groupPrice,String groupMembers,String openid){
        try{
            SysWechatConfig config = weChatConfigUtil.getWeChatByAppid(appId);
            WeChat weChat = new WeChat(config.getAppId(), config.getAppSecret());
            String accessToken = weChatOverUtil.getAccessToken(weChat);

            Map<String,Object> dataMap = new HashMap<>();
            Map<String,Object> first = new HashMap<>();
            first.put("value","恭喜您拼团成功啦~");
            Map<String,Object> keyword1 = new HashMap<>();
            keyword1.put("value",goodsName);
//            keyword1.put("color","#173177");
            Map<String,Object> keyword2 = new HashMap<>();
            keyword2.put("value",groupPrice);
            Map<String,Object> keyword3 = new HashMap<>();
            keyword3.put("value",groupMembers);
            Map<String,Object> remark = new HashMap<>();
            remark.put("value","您的拼团于" + DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss") + "完成，请及时下单哟~");

            dataMap.put("first",first);
            dataMap.put("keyword1",keyword1);
            dataMap.put("keyword2",keyword2);
            dataMap.put("keyword3",keyword3);
            dataMap.put("remark",remark);
            JSONObject jsonObject = new JSONObject(dataMap);
            WeixinTemplateSender.sendWechatmsgToUser(openid,WeChatMsgTemplates.groupSuccess,"",jsonObject,accessToken);
        }catch (Exception e){
            e.printStackTrace();
            log.error("推送用户失败",e);
        }

    }

    //砍价完成
    public void kanjiaSuccess(String goodsName,String kanjiaPrice,String openid){
        try{
            SysWechatConfig config = weChatConfigUtil.getWeChatByAppid(appId);
            WeChat weChat = new WeChat(config.getAppId(), config.getAppSecret());
            String accessToken = weChatOverUtil.getAccessToken(weChat);

            Map<String,Object> dataMap = new HashMap<>();
            Map<String,Object> first = new HashMap<>();
            first.put("value","您的砍价已完成。");
            Map<String,Object> keyword1 = new HashMap<>();
            keyword1.put("value",goodsName);
//            keyword1.put("color","#173177");
            Map<String,Object> keyword2 = new HashMap<>();
            keyword2.put("value",kanjiaPrice);
            Map<String,Object> remark = new HashMap<>();
            remark.put("value","您的砍价于" + DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss") + "完成，请及时下单哟~");

            dataMap.put("first",first);
            dataMap.put("keyword1",keyword1);
            dataMap.put("keyword2",keyword2);
            dataMap.put("remark",remark);
            JSONObject jsonObject = new JSONObject(dataMap);
            WeixinTemplateSender.sendWechatmsgToUser(openid,WeChatMsgTemplates.kanjiaSuccess,"",jsonObject,accessToken);
        }catch (Exception e){
            e.printStackTrace();
            log.error("推送用户失败",e);
        }

    }
}
